package com.rds.benchmark.operator;

import com.rds.benchmark.util.MakeValue;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.commands.JedisCommands;

import java.io.IOException;
import java.util.HashMap;

public class RedisBenchmarkOperator extends TypeOperatorBase {
    private Object conn;

    private String key;

    private String mode;

    private String value = MakeValue.value;

    private int SUBCONTENTS = 1000;
//
    private HashMap<String,String> map;

    private int idx;
    private String mk;
    private boolean flag = false;

    public void setFlag(boolean flag){
        this.flag = flag;
    }

    public void setNumber(int number) {
        SUBCONTENTS = number;
    }


    @Override
    public void init(Object conn, String key, int idx, String mode) {
        this.conn = conn;
        this.key = key;
        this.mode = mode;
        this.idx = idx;
//        this.mk = "thread-123456789012345678901234567890123456789012345678901234567890-" + this.idx + "-" + this.SUBCONTENTS;
        this.mk = this.key + "-" + this.idx + "-" + this.SUBCONTENTS;
        //要计算循环数拼接到value中
        if(flag) {
            String num = String.valueOf(SUBCONTENTS);
            int valueLength = value.length();
            value = value.substring(0, valueLength - num.length() - 5) + "value" + num;
        }
        map = new HashMap<String,String>();
        map.put(mk,value);
    }

    @Override
    public Long clear() {
        return ((JedisCommands) conn).del(key);
    }

    @Override
    public void operate() throws IOException {
        if (conn instanceof JedisCommands) {
            Jedis jedis = (Jedis) conn;
            if (mode != null && mode.equals("set")) {
                jedis.set(key, value);
                times += 1;
            } else if (mode != null && mode.equals("get")) {
                jedis.get(key);
                times += 1;
            } else if (mode != null && mode.equals("scan")) {
                jedis.scan("0");
                times += 1;
            } else if (mode != null && mode.equals("del")) {
                jedis.del(key);
                times += 1;
            } else if (mode != null && mode.equals("incr")) {
                jedis.incr(key);
                times += 1;
            } else if (mode != null && mode.equals("hset")) {
                jedis.hset(key, mk, value);
                times += 1;
            } else if (mode != null && mode.equals("hget")) {
                jedis.hget(key, mk);
                times += 1;
            } else if (mode != null && mode.equals("hgetall")) {
                jedis.hgetAll(key);
                times += 1;
            } else if (mode != null && mode.equals("hscan")) {
                jedis.hscan(key, "0");
                times += 1;
            } else if (mode != null && mode.equals("hdel")) {
                jedis.hdel(key, mk);
                times += 1;
            } else if (mode != null && mode.equals("lpush")) {
                jedis.lpush(key, value);
                times += 1;
            } else if (mode != null && mode.equals("lpop")) {
                jedis.lpop(key);
                times += 1;
            } else if (mode != null && mode.equals("rpush")) {
                jedis.rpush(key, value);
                times += 1;
            } else if (mode != null && mode.equals("rpop")) {
                jedis.rpop(key);
                times += 1;
            } else if (mode != null && mode.equals("lrange")) {
                jedis.lrange(key, 0, 100);
                times += 1;
            } else if (mode != null && mode.equals("sort")) {
                jedis.sort(key);
                times += 1;
            } else if (mode != null && mode.equals("lrem")) {
                jedis.lrem(key, 1, value);
                times += 1;
            } else if (mode != null && mode.equals("sadd")) {
                jedis.sadd(key, value);
                times += 1;
            } else if (mode != null && mode.equals("smembers")) {
                jedis.smembers(key);
                times += 1;
            } else if (mode != null && mode.equals("sscan")) {
                jedis.sscan(key, "0");
                times += 1;
            } else if (mode != null && mode.equals("srem")) {
                jedis.srem(key, value);
                times += 1;
            } else if (mode != null && mode.equals("spop")) {
                jedis.spop(key);
                times += 1;
            } else if (mode != null && mode.equals("sismember")) {
                jedis.sismember(key, value);
                times += 1;
            } else if (mode != null && mode.equals("srandmember")) {
                jedis.srandmember(key);
                times += 1;
            } else if (mode != null && mode.equals("zadd")) {
                jedis.zadd(key, SUBCONTENTS, value);
                times += 1;
            } else if (mode != null && mode.equals("zrange")) {
                jedis.zrange(key, 0, 100);
                times += 1;
            } else if (mode != null && mode.equals("zrevrange")) {
                jedis.zrevrange(key, 0, 100);
                times += 1;
            } else if (mode != null && mode.equals("zunionstore")) {
                jedis.zunionstore(key, mk);
                times += 1;
            } else if (mode != null && mode.equals("zinterstore")) {
                jedis.zinterstore(key, mk);
                times += 1;
            } else if (mode != null && mode.equals("zscan")) {
                jedis.zscan(key, "0");
                times += 1;
            } else if (mode != null && mode.equals("zrem")) {
                jedis.zrem(key, mk);
                times += 1;
            } else if (mode != null && mode.equals("ping")) {
                jedis.ping();
                times += 1;
            } else if (mode != null && mode.equals("mset")) {
                jedis.mset(key, value);
                times += 1;
            } else if (mode != null && mode.equals("hincrby")) {
                jedis.hincrBy(key, mk, 1);
                times += 1;
            } else if (mode != null && mode.equals("hmset")) {
                jedis.hmset(key, map);
                times += 1;
            } else if (mode != null && mode.equals("zpopmin")) {
                jedis.zpopmin(key);
                times += 1;
            } else if (mode != null && mode.equals("zpopmax")) {
                jedis.zpopmax(key);
                times += 1;
            }
        }
    }
}
